

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <stdarg.h>

void write_log2file(char *pcLogContent)
{
    char  szLogName[256];
    FILE  *pFile = NULL;

    time_t t = time(0);
    struct tm *pTempTm = localtime(&t);
    sprintf(szLogName, "./%04d%02d%02d.log", 
            pTempTm->tm_year+1900, 
            pTempTm->tm_mon + 1, 
            pTempTm->tm_mday);

    pFile = fopen(szLogName, "a+");
    if (NULL != pFile)
    {
        fprintf(pFile, "%s\n", pcLogContent);
        fclose(pFile);
    }

    return ;
}

void write_log(const char* filename, const int fileline, const char* fmt, ...)
{
    char szLogText[512];
    struct tm *pTempTm = NULL;
    int iLen = 0;

    time_t t = time(0);
    pTempTm = localtime(&t);

    sprintf(szLogText, "[%02d:%02d:%02d] %s:%d ", pTempTm->tm_hour, 
            pTempTm->tm_min + 1, pTempTm->tm_sec, filename, fileline);
    iLen = strlen(szLogText);

    va_list argptr;    
    va_start(argptr, fmt);    
    vsnprintf(szLogText+iLen, sizeof(szLogText)-iLen, fmt, argptr);
    va_end(argptr);

    write_log2file(szLogText);
    
    return ;
}

